﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Xsl;
using System.Xml.XPath;
using System.Xml;
using System.Diagnostics;
using System.Threading;
using System.IO;
using System.Net.Mail;
using System.Net;
using Logging = Microsoft.Practices.EnterpriseLibrary.Logging;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;

namespace CokeZeroCodeUITest
{
    class Program
    {

        private static string testResultFile = string.Empty;

        static void Main(string[] args)
        {

            XmlDocument doc = new XmlDocument();
            string testContainer = String.Empty;
            string VSPath = String.Empty;
            try
            {
                doc.Load("Config.xml");
                XmlNode DLLNode = doc.SelectSingleNode("root/TestDLL");
                XmlNode VSpath = doc.SelectSingleNode("root/VSpath");
                testContainer = DLLNode.InnerText;
                VSPath = VSpath.InnerText;
            }
            catch (Exception e)
            {
                Log("Can't load Config XML file!");
                return;
            }
            
            if (string.IsNullOrEmpty(testContainer))
            {
                throw new ArgumentNullException("testcontainer");
            }

            testResultFile = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "testResult_" + DateTime.Now.ToString("yyyy_MM_dd HH_mm_ss") + ".trx");
            Process p = new Process();
            p.EnableRaisingEvents = true;
            p.StartInfo.FileName = "cmd.exe";
            p.StartInfo.CreateNoWindow = true;
            p.StartInfo.RedirectStandardInput = true;
            p.StartInfo.UseShellExecute = false;
            p.StartInfo.RedirectStandardOutput = true;
            p.StartInfo.RedirectStandardError = true;
            p.Exited += new EventHandler(p_Exited);
            p.Start();
            Log("Start to run tests, please wait....");
            p.StandardInput.WriteLine(@"cd " + VSPath);
            string diskPath = VSPath.Substring(0, 2);
            p.StandardInput.WriteLine(diskPath);
            p.StandardInput.WriteLine(@"MSTest " + "/testcontainer:\"" + testContainer + "\" /resultsfile:\"" + testResultFile + "\"");
            p.StandardInput.WriteLine("exit");

            var s = p.StandardOutput.ReadToEnd();
            var error = p.StandardError.ReadToEnd();

            p.WaitForExit();
            p.Close();

            if (!string.IsNullOrEmpty(error))
            {
                Log("Processing failed due to......");
                Console.WriteLine("Error: " + error);
            }
            else
            {
                Log("Processing complete......");
            }

            Log("Press Enter key to exit...");
            //Console.ReadLine();
        }

        static void p_Exited(object sender, EventArgs e)
        {
            Log("Run tests complete, begin to convert test results...");
      
            string body = string.Empty;
            try
            {
                body = Xml2Html.Transform("Trx2Html.xslt", testResultFile);
                Log("Convert test results complete...");
            }
            catch (Exception ex)
            {
                Log("Convert test results failed due to...");
                Console.WriteLine("Error: " + ex.Message);
                return;
            }

            Log("Begin to send mail...");
   
            try
            {
                SendMail sm = new SendMail();
                sm.MailProperties(body);
                sm.Send();
            }
            catch (Exception ex)
            {
                Log("Send mail failed due to...");
                Console.WriteLine("Error: " + ex.Message);
            }
        }

        static void Log(string message)
        {
            Console.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss ") + " " + message);
        }

        public static string TestMethod()
        {
            string body = Xml2Html.Transform("Trx2Html.xslt", "testResult_2011_11_02 16_40_02.trx");
            return body;
        }
    }
}
